<?php

namespace App\Service\Workflow\Type;

use App\Enums\Workflow\WorkflowEnum;
use App\Models\Workflow\WorkflowOrderLogModel;
use App\Models\Workflow\WorkflowOrderModel;
use App\Models\Workflow\WorkflowOrderNodeModel;
use App\Service\BaseService;
use App\Service\Workflow\WorkflowOrderService;

abstract class WorkflowBaseTypeService
{
    protected WorkflowOrderModel      $workflowOrderModel;
    protected ?WorkflowOrderNodeModel $currentNode;
    protected WorkflowOrderService    $workflowOrderService;

    protected function __construct(WorkflowOrderService $workflowOrderService, ?WorkflowOrderNodeModel $currentNode)
    {
        $this->workflowOrderService = $workflowOrderService;

        $this->workflowOrderModel = $this->workflowOrderService->getWorkflowOrderModel();

        // 当前节点
        if ($currentNode) {
            $this->currentNode = $currentNode;
        } else {
            $this->currentNode = $this->workflowOrderService->getCurrentMainNode();
        }
    }

    /**
     * 实例化子类
     * @param int $workflowType
     * @param WorkflowOrderModel $workflowOrderModel
     * @param WorkflowOrderNodeModel|null $workflowOrderNodeModel
     * @return RenewTypeService|mixed
     */
    public static function getIns(int $workflowType, WorkflowOrderService $workflowOrderService, ?WorkflowOrderNodeModel $currentNode): self {
        switch ($workflowType) {
            case WorkflowEnum::WORKFLOW_TYPE_OPEN_SHOP_RENEW:
                $class = RenewTypeService::class;
                break;
            default:
                BaseService::throwErr('WorkflowBaseTypeService未配置的workflowType:' . $workflowType);
        }

        return new $class($workflowOrderService, $currentNode);
    }

    /**
     * 节点完成后干的事
     */
    abstract protected function finishNode(int $auditStatus = WorkflowOrderLogModel::AUDIT_DEFAULT);
}
